-- Rogue Rage
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Darren Grey
-- http://gruesomegames.com

require "engine.class"
local Dialog = require "engine.ui.Dialog"
local Textzone = require "engine.ui.Textzone"
local Separator = require "engine.ui.Separator"
local List = require "engine.ui.List"
local Savefile = require "engine.Savefile"
local Map = require "engine.Map"

module(..., package.seeall, class.inherit(Dialog))

function _M:init(actor)
	self.actor = actor
	Dialog.init(self, "Game Over", 500, 500)
	
	game:stopMusic()
	game:playSound("death")

	local stuff = "Dead."
	
	if not world.best_score then world.best_score = 0 end

	if game.player.score > world.best_score then
		local new_hi_score = true
		world.best_score = game.player.score
		world:saveWorld()
		stuff = ("You have #LIGHT_RED#died#LAST#.  The jam has won!\n\nYou scored #YELLOW#%d#LAST# points, a new high score!\n"):format(game.player.score)
	else
		stuff = ("You have #LIGHT_RED#died#LAST#.  The jam has won!\n\nYou scored #YELLOW#%d#LAST# points, miserably failing to beat your high score of #YELLOW#%d#LAST.\n"):format(game.player.score,world.best_score)
	end

	self:generateList()
		
	self.c_desc = Textzone.new{width=self.iw, auto_height=true, text = stuff}
	self.c_list = List.new{width=self.iw, nb_items=#self.list, list=self.list, fct=function(item) self:use(item) end}

	self:loadUI{
		{left=0, top=0, ui=self.c_desc},
		{left=5, top=self.c_desc.h, padding_h=10, ui=Separator.new{dir="vertical", size=self.iw - 10}},
		{left=0, bottom=0, ui=self.c_list},
	}
	self:setFocus(self.c_list)
	self:setupUI(false, true)
end

function _M:use(item)
	if not item then return end
	local act = item.action

   if act == "dump" then
      game:registerDialog(require("mod.dialogs.CharacterSheet").new(game.player))
	elseif act == "exit" then
		local save = Savefile.new(game.save_name)
		save:delete()
		save:close()
		if item.subaction == "none" then
			util.showMainMenu()
		elseif item.subaction == "restart-new" then
			util.showMainMenu(false, engine.version[4], engine.version[1].."."..engine.version[2].."."..engine.version[3], game.__mod_info.short_name, game.save_name, true)
		end
	elseif act == "cheat" then
		game.logPlayer(self.actor, "#LIGHT_BLUE#You resurrect! CHEATER !")

		self:cleanActor()
		self:restoreResources()
		self:resurrectBasic()
	end
end

function _M:generateList()
	local list = {}

	list[#list+1] = {name="Restart the game and do better this time", action="exit", subaction="restart-new"}
	list[#list+1] = {name="Sure just one more try won't hurt", action="exit", subaction="restart-new"}
	list[#list+1] = {name="Ah go on, have another game!", action="exit", subaction="restart-new"}
	list[#list+1] = {name="Go on, go on, go on, go on...", action="exit", subaction="restart-new"}
	list[#list+1] = {name="Quit like a coward", action="exit", subaction="none"}

	self.list = list
end
